<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active{
      color:red;
    }
    .news .router-link-exact-active {
      color:rgb(60, 255, 0);
    }
  </style>
</head>
<body>
  <div id="app">
    <router-link to="/home" tag="button">首页</router-link>
    <router-link to="/news" tag="button">新闻</router-link>
    <router-link to="/about" tag="button">关于我们</router-link>
    <router-view/>
  </div>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>
  <script>
    // 创建 路由组件option
    const Home = {
      template: `
        <div>
          <h2>首页</h2>  
        </div>
      `
    }
    const News = {
      template: `
        <div class="news">
          <h2>新闻</h2>  
          <router-link to="/news/native">国内新闻</router-link>
          <router-link to="/news/abroad">国外新闻</router-link>
          <router-view></router-view>
        </div>
      `
    }
    const About = {
      template: `
        <div>
          <h2>关于我们</h2>  
        </div>
      `
    }
    const NotFound =  {
      template: `
        <div>
          <h2>404了</h2>  
        </div>
      `
    }
    const NativeNews =  {
      template: `
        <div>
          <h3>国内新闻</h3>  
        </div>
      `
    }
    const AbroadNews =  {
      template: `
        <div>
          <h3>国外新闻</h3>  
        </div>
      `
    }
    const routes = [
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home',
        component: Home
      },
      {
        path: '/news',
        // 父级路由规则中新增 children属性 定义子路由   
        children: [
          // 子路由path 建议 携带 父路由地址做为第一个path 
          {
            path: '/news',
            redirect: '/news/native'
          },
          {
            path: '/news/native',
            component: NativeNews
          },
          {
            path: '/news/abroad',
            component: AbroadNews
          }
        ],
        component: News
      },
      {
        path: '/about',
        component: About
      },
      {
        path: '*',
        component: NotFound
      }
    ]
    //  创建router 实例
    const router = new VueRouter({
      routes
    })
    const vm = new Vue({
      el: '#app',
      router
    })
  </script>
</body>
</html>